package com;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.utils.Regulations;
import com.utils.ScannerUtils;

public class Hand {

	private List<Card> cards;
	public final int NUMBER_OF_CARDS = 52;
	public final int NUMBER_OF_SUITS = 4;
	public final int NUMBER_OF_RANKS = 13;

	private Player player1;
	private Player player2;
	private Player player3;
	private Player player4;

	private Team team1;
	private Team team2;

	private static Suit trump = Suit.spade;
	private static Suit handCard;

	public Hand(Player player1, Player player2, Player player3, Player player4,
			Team team1, Team team2) {
		super();

		this.player1 = player1;
		this.player2 = player2;
		this.player3 = player3;
		this.player4 = player4;

		this.team1 = team1;
		this.team2 = team2;

		List<Card> cards1 = new ArrayList<Card>();
		List<Card> cards2 = new ArrayList<Card>();
		List<Card> cards3 = new ArrayList<Card>();
		List<Card> cards4 = new ArrayList<Card>();

		cards = createCards();
		cards = shuffleCards(cards);

		for (int i = 0; i < 3; i++) {
			if (i == 0) { // Give 5 cards
				for (int j = 0; j < 5; j++) {
					cards1.add(cards.get(j));
				}
				for (int j = 5; j < 10; j++) {
					cards2.add(cards.get(j));
				}
				for (int j = 10; j < 15; j++) {
					cards3.add(cards.get(j));
				}
				for (int j = 15; j < 20; j++) {
					cards4.add(cards.get(j));
				}
			} else if (i == 1) { // Give 4 cards
				for (int j = 20; j < 24; j++) {
					cards1.add(cards.get(j));
				}
				for (int j = 24; j < 28; j++) {
					cards2.add(cards.get(j));
				}
				for (int j = 28; j < 32; j++) {
					cards3.add(cards.get(j));
				}
				for (int j = 32; j < 36; j++) {
					cards4.add(cards.get(j));
				}
			} else if (i == 2) { // Second 4 cards
				for (int j = 36; j < 40; j++) {
					cards1.add(cards.get(j));
				}
				for (int j = 40; j < 44; j++) {
					cards2.add(cards.get(j));
				}
				for (int j = 44; j < 48; j++) {
					cards3.add(cards.get(j));
				}
				for (int j = 48; j < 52; j++) {
					cards4.add(cards.get(j));
				}
			}
		}

		this.player1.setCards(cards1);
		this.player2.setCards(cards2);
		this.player3.setCards(cards3);
		this.player4.setCards(cards4);

	}

	public List<Card> getCards() {
		return cards;
	}

	public void setCards(List<Card> cards) {
		this.cards = cards;
	}

	public static Suit getTrump() {
		return trump;
	}

	public static Suit getHandCard() {
		return handCard;
	}

	public List<Card> createCards() {
		cards = new ArrayList<Card>();

		int suitCounter = 0;
		int rank = 1;

		for (int i = 0; i < NUMBER_OF_CARDS; i++) {
			Card card = new Card();

			if (suitCounter < NUMBER_OF_RANKS) {
				card.setSuit(Suit.spade);
			} else if (suitCounter < NUMBER_OF_RANKS * 2) {
				card.setSuit(Suit.club);
			} else if (suitCounter < NUMBER_OF_RANKS * 3) {
				card.setSuit(Suit.heart);
			} else if (suitCounter < NUMBER_OF_RANKS * 4) {
				card.setSuit(Suit.diamond);
			}

			card.setRank(rank);
			rank++;
			suitCounter++;

			if (rank == 14) {
				rank = 1;
			}

			cards.add(card);

		}

		return cards;
	}

	public void startHand() {
		trump = makeTrump();

		int counter = 1;
		while (counter < 14) {
			counter++;
			Card card1 = player1.playCard();
			handCard = card1.getSuit();
			Card card2 ;
			Card card3; 
			Card card4;
			while(true){
				 card2 = player2.playCard();
				 
				break;
			}
			while(true){
				 card3 = player3.playCard();
				 break;
			}
			while(true){
			 card4 = player4.playCard();
			 break;
			}
			Card winner = Regulations.retrieveWinnerCard(card1, card2, card3,
					card4);

			if (winner.equals(card1)) {
				team1.setCurrentScore(team1.getCurrentScore() + 1);
			} else if (winner.equals(card2)) {
				team2.setCurrentScore(team2.getCurrentScore() + 1);
			} else if (winner.equals(card3)) {
				team1.setCurrentScore(team1.getCurrentScore() + 1);
			} else {
				team2.setCurrentScore(team2.getCurrentScore() + 1);
			}

			System.out.println(team1.getCurrentScore());
			System.out.println(team2.getCurrentScore());
		}
	}

	private Suit makeTrump() {
		for (int i = 0; i < 5; i++) {
			System.out.print(player1.getCards().get(i).getRank()
					+ player1.getCards().get(i).getSuit().name()
							.substring(0, 1).toUpperCase() + "\t");

		}
		System.out.println();
		int trumpIndex = ScannerUtils.readFromKeyboard(5);

		return player1.getCards().get(trumpIndex).getSuit();
	}

	public List<Card> shuffleCards(List<Card> cardsDeck) {
		for (int i = 0; i < cardsDeck.size(); i++) {
			int random = new Random().nextInt(NUMBER_OF_CARDS);		
			Card temp = cardsDeck.get(i);
			cardsDeck.set(i, cardsDeck.get(random));
			cardsDeck.set(random, temp);
		}
		return cardsDeck;
	}
}